<!-- 新增和编辑弹窗 -->
<template>
  <a-modal
    :width="1050"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate ? '客户详情' : '新建代理商'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-card :bordered="false">
      <a-descriptions bordered :column="6" size="small">
        <a-descriptions-item label="客户名称" :span="3">{{ form.name }}</a-descriptions-item>
        <a-descriptions-item label="所属代理商" :span="3">{{ form.agentName }}</a-descriptions-item>
        <a-descriptions-item label="客户类型" :span="3">{{ form.typeName }}</a-descriptions-item>
        <a-descriptions-item label="负责员工" :span="3">{{ form.createUserName }}</a-descriptions-item>
        <a-descriptions-item label="客户分类" :span="3">{{ form.classify }}</a-descriptions-item>
        <a-descriptions-item label="所在地区" :span="3">{{ form.address }}</a-descriptions-item>
        <a-descriptions-item v-if="form.type === 1" label="统一社会信用代码" :span="3">{{ form.uscc }}</a-descriptions-item>
        <a-descriptions-item v-if="form.type === 2" label="手机号码" :span="3">{{ form.phone }}</a-descriptions-item>
        <a-descriptions-item label="详细地址" :span="3">{{ form.addressDetail }}</a-descriptions-item>
        <a-descriptions-item label="客户等级" :span="3"> <a-rate :value="form.level" disabled /></a-descriptions-item>
        <a-descriptions-item label="获得时间" :span="3">{{ form.createTime }}</a-descriptions-item>
        <a-descriptions-item label="备注" :span="6">{{ form.remark }}</a-descriptions-item>
      </a-descriptions>
      <a-table row-key="id"
               :columns="columns"
               :data-source="contactData"
               :pagination="false"
               :size="'small'"
               :bordered="true"
               :scroll="{ y: 230 }"
                style="margin-top: 30px;">
        <template #title>
          <h5>联系人信息</h5>
        </template>
      </a-table>
    </a-card>
    <template #footer>
      <div style="text-align: right">
        <a-button :type="'primary'" @click="updateVisible(false)" style="margin-left: 20px">关闭</a-button>
      </div>
    </template>
  </a-modal>
</template>

<script>
import { onMounted, ref } from 'vue';
import { AgentApi } from '@/api/AgentApi';
import { CustomerContactsApi } from '@/api/CustomerContactsApi';

export default {
  name: 'AgentAccountEdit',
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  setup() {

    // 加载代理商下拉数据
    const agentList = ref([]);

    async function fetchAgentList() {
      const res = await AgentApi.findList({});
      agentList.value = res.data;
    }


    // 页面初次挂载时加载下拉数据
    onMounted(async () => {
      await fetchAgentList();
    });

    return {
      agentList
    };
  },
  data() {

    // 加载经理候选人下拉数据
    let jlList;
    return {
      jlList,
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        agentId: [{ required: true, message: '请选择代理商', type: 'string', trigger: 'blur' }]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,

      // 联系人
      contactData: [],
      columns: [
        {
          title: '姓名',
          dataIndex: 'name',
          width: 160,
          slots: { customRender: 'sort' }
        },
        {
          title: '电话',
          dataIndex: 'phone',
          width: 160
        },
        {
          title: '邮箱',
          dataIndex: 'email',
          ellipsis: true,
          width: 160
        },
        {
          title: '备注',
          dataIndex: 'remark',
          ellipsis: true,
          width: 160
        }
      ]

    };
  },
  watch: {
    visible() {
      if (this.data) {
        // 查询联系人详情
        this.getContactData();
      }
    },

    data() {
      if (this.data) {
        // 编辑的时候适配组件数据类型
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        // 新增-清除表格内容
        this.form = {};
        this.isUpdate = false;
      }
      if (this.$refs.form) {
        this.$refs.form.clearValidate();
      }
    }
  },
  methods: {
    // 联系人
    async getContactData() {
      const search = {
        customerId:this.data.id,
        pageNo: 1,
        pageSize: 100
      };
      const res = await CustomerContactsApi.findPage(search);
      this.contactData = res.data.rows;
    },

    /**
     * 保存
     */
    async save() {
      // 关闭弹框，通过控制visible的值，传递给父组件
      this.updateVisible(false);
      // 触发父组件done事件
      this.$emit('done');
    },

    /**
     * 更新编辑界面的弹框是否显示
     * @param value true-显示，false-隐藏
     */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }

  }
};
</script>

<style scoped>
</style>
